class Solution:
    """
    605. 种花问题
    https://leetcode-cn.com/problems/can-place-flowers/
    @date: 2021年1月2日15:50:00
    @author: mizzle rain
    """

    def canPlaceFlowers(self, flowerbed, n):
        if n == 0:
            return True
        size = len(flowerbed)
        if size == 1:
            return n <= 1 and flowerbed[0] == 0
        for i in range(size):
            f = flowerbed[i]
            if i == 0:
                if flowerbed[i + 1] == 0 and flowerbed[i] == 0:
                    n -= 1
                    flowerbed[i] = 1
            elif i == size - 1:
                if flowerbed[i-1] == 0 and flowerbed[i] == 0:
                    n -= 1
            else:
                if flowerbed[i-1] == 0 and flowerbed[i+1] == 0 and flowerbed[i] == 0:
                    n -= 1
                    flowerbed[i] = 1
        return n <= 0


if __name__ == "__main__":
    s = Solution()
    ans = s.canPlaceFlowers([1, 0, 0, 0, 1], 2)
    print(ans)
